Mixes up the cells of target, in place: afterwards every cell holds one of the values the grid held before, in a random place. Cells move one at a time, so a row or a column does not stay together (use grid_shuffle_rows or grid_shuffle_columns for that). It makes no copy.
grid_shuffle(target)| Name | Type |
|---|---|
target | grid |
Nothing.
Mixes up the cells of target, a grid. Afterwards every cell holds one of the values the grid held before, in a random place. Every cell is moved on its own, so a row or a column does not stay together.
grid_shuffle(board);There are two more shuffles for a grid that keep things together. grid_shuffle_rows moves whole rows and grid_shuffle_columns moves whole columns.
| You want | Use |
|---|---|
| every cell in a random place | grid_shuffle |
| the rows in a random order, each row staying as it is | grid_shuffle_rows |
| the columns in a random order, each column staying as it is | grid_shuffle_columns |
grid_shuffle moves the values that are already in target. It does not make a new grid, and it needs no extra memory. The old arrangement is gone afterwards. If you want to keep it, make a copy with grid_copy first and shuffle the one you want mixed up.
The grid keeps its width and height. Any kind of value can be shuffled: numbers, text, structs, references. Nothing is compared, so there is no rule about what the grid holds. (A sort is different: it only works on numbers and text.)
A shuffle picks its random numbers from the same place roll_int does: one stream of random numbers for the whole game. There is no seed to give the shuffle itself. What follows from that:
roll_reseed changes it. Calling roll_reseed with a number starts the stream again from that number, so roll_reseed(7) just before grid_shuffle gives the same shuffle every time you do it with the grid in the same arrangement to start with. Use that when you want a shuffle you can repeat. For a different shuffle each game, reseed from something the player decides, such as how long they waited before pressing a button (see roll_reseed).roll_int, roll_real and roll_chance that comes after it.Every possible arrangement can come up. It is very slightly more likely to land on some than others, because a random number is cut down to fit a small range, but the difference is far too small for a game to show.
| You do this | What happens |
|---|---|
| shuffle a grid with one cell | nothing changes, and no random numbers are used |
| shuffle a grid that has the same value more than once | works as normal. Equal values are interchangeable, so you cannot tell which is which |
| shuffle and hope a cell keeps its value | it may. That can happen with random order, and it gets less likely the more cells there are |
| give it a list or an array | refused when checked, and the message names the function that shuffles that kind |
| use it where a value is needed | refused when checked. It gives nothing back |
A script that takes a grid as a parameter works on the caller's own grid (see Grids). grid_shuffle needs no width or height, because it moves cells one at a time, so unlike grid_shuffle_rows it works inside a script, on the caller's grid, whatever size that is.
Nothing is set aside: the cells are swapped where they stand, one swap for each cell after the first. No timing was measured.
var board = grid_create(3, 3, 0);
board[# 0, 0] = 1;
board[# 1, 1] = 2;
board[# 2, 2] = 3;
grid_shuffle(board); // the 1, the 2, the 3 and six 0s, in new placesShuffles every cell of the vector as one long run, row by row. It needs no width or height, so, like grid_fill, it also works inside a script on the CALLER's grid, whatever size that is. A Fisher-Yates shuffle done in place: it makes no copy and needs no extra memory. For each place from the last down to the second, it swaps that place with one picked at random from the places up to and including it, so every order can come up. The pick comes from gbml::roll_int, the one stream the roll_ functions use, so a shuffle follows the same rules: the same ROM shuffles the same way every time it is switched on, roll_reseed changes that, and roll_reseed with the same number just before a shuffle gives the same shuffle again. A shuffle uses up rolls from that stream (one for each place after the first), so it moves every later roll along. Nothing is compared, so any kind of item can be shuffled.